package router

import (
	"net/http"
	"goo/apis/user"
	"github.com/gin-gonic/gin"
)

func InitRouter() *gin.Engine {
	router := gin.Default()

	// 无参数
	router.GET("/user/login", user.Login)
	// 首页
	router.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "who i am ")
	})

	// 带参数
	router.GET("/user/:name", func(c *gin.Context) {
		name := c.Param("name")
		// role := c.DefaultQuery("role", "m默认值")
		c.String(http.StatusOK, "你就是个 %s", name)
	})

	router.GET("/users", func(c *gin.Context) {
		name := c.Query("name")
		role := c.DefaultQuery("role", "teacher")
		c.String(http.StatusOK, "%s is a %s", name, role)
	})

	router.POST("form", func(c *gin.Context) {
		name := c.DefaultPostForm("name", "深谙")
		sex := c.DefaultPostForm("sex", "性别")
		c.JSON(http.StatusOK, gin.H{
			"name" : name,
			"sex" : sex,
		})
	})

	router.GET("/getUserInfo", user.GetUserInfo)

	return router
}

